﻿@page "/Bas/CodingRule/Index"
@using nERP.ViewModel.Bas.CodingRuleVMs
@using System.ComponentModel.DataAnnotations
@inherits BasePage
@attribute [ActionDescription("_Page.Bas.CodingRule.Index", "nERP.Bas.Controllers,CodingRule")]

<Table @ref="CodingRuleListVMdataTable" TItem="nERP.ViewModel.Bas.CodingRuleVMs.CodingRule_View" OnQueryAsync="CodingRuleListVMOnSearchCodingRule"
       IsStriped="true" IsBordered="true" ShowRefresh="false" ShowExtendEditButton="false" ShowExtendDeleteButton="false"
       ShowToolbar="true" IsMultipleSelect="true" ShowExtendButtons="true" TableSize="TableSize.Compact"
       AllowResizing="true" ShowDefaultButtons="false" IsPagination="true" AllowDragColumn="true" ShowColumnList="true" ShowSearch="true" SearchTooltip=""
       OnResetSearchAsync="OnResetSearchAsync" SearchMode="SearchMode.Top" CollapsedTopSearch="true" ShowSearchText="false">
    <SearchTemplate>
        <ValidateForm Model="@Model">
            <Row ItemsPerRow="ItemsPerRow.Three" RowType="RowType.Inline">
                <Select @bind-Value="@Model.Searcher.CodingListId" Items="AllCodingLists" PlaceHolder="@WtmBlazor.Localizer["Sys.All"]" Id="g3872485ac6f44245abaab333d7c55fcd" />
                <BootstrapInput @bind-Value="@Model.Searcher.Seq" />
                <Select @bind-Value="@Model.Searcher.CodeRule" PlaceHolder="@WtmBlazor.Localizer["Sys.All"]" Id="gbb7711a6dbc5453780d52a18706c334c" />
                <BootstrapInput @bind-Value="@Model.Searcher.FixedValue" />
                <Select @bind-Value="@Model.Searcher.FixedRule" PlaceHolder="@WtmBlazor.Localizer["Sys.All"]" Id="g746c4daed9be4a9a95cd3c92b455b600" />
                <BootstrapInput @bind-Value="@Model.Searcher.GlideLen" />
                <BootstrapInput @bind-Value="@Model.Searcher.StartNumber" />
                <BootstrapInput @bind-Value="@Model.Searcher.GlideStep" />
                <BootstrapInput @bind-Value="@Model.Searcher.Glide" />
            </Row>
        </ValidateForm>
    </SearchTemplate>
    <TableColumns>
        <TableColumn @bind-Field="@context.CodingRule_CodingList" Text="@WtmBlazor.Localizer["Page.单据编码规则主表"]" />
        <TableColumn @bind-Field="@context.CodingRule_Seq" Text="@WtmBlazor.Localizer["Page.序号"]" />
        <TableColumn @bind-Field="@context.CodingRule_CodeRule" Text="@WtmBlazor.Localizer["Page.规则"]" />
        <TableColumn @bind-Field="@context.CodingRule_FixedValue" Text="@WtmBlazor.Localizer["Page.固定值"]" />
        <TableColumn @bind-Field="@context.CodingRule_FixedRule" Text="@WtmBlazor.Localizer["Page.规则值"]" />
        <TableColumn @bind-Field="@context.CodingRule_GlideLen" Text="@WtmBlazor.Localizer["Page.流水号长度"]" />
        <TableColumn @bind-Field="@context.CodingRule_StartNumber" Text="@WtmBlazor.Localizer["Page.流水号起始值"]" />
        <TableColumn @bind-Field="@context.CodingRule_GlideStep" Text="@WtmBlazor.Localizer["Page.递增步长"]" />
        <TableColumn @bind-Field="@context.CodingRule_Glide" Text="@WtmBlazor.Localizer["Page.流水号补位码"]" />
        <TableColumn @bind-Field="@context.CodingRule_CreateTime" Text="@WtmBlazor.Localizer["_Admin.CreateTime"]"  FormatString="yyyy-MM-dd HH:mm:ss"/>
        <TableColumn @bind-Field="@context.CodingRule_UpdateTime" Text="@WtmBlazor.Localizer["_Admin.UpdateTime"]" FormatString="yyyy-MM-dd HH:mm:ss" />
        <TableColumn @bind-Field="@context.CodingRule_CreateBy" Text="@WtmBlazor.Localizer["_Admin.CreateBy"]" />
        <TableColumn @bind-Field="@context.CodingRule_UpdateBy" Text="@WtmBlazor.Localizer["_Admin.UpdateBy"]" />
        <TableColumn @bind-Field="@context.CodingRule_IsValid" Text="@WtmBlazor.Localizer["_Admin.IsValid"]" />
    </TableColumns>

    <RowButtonTemplate>
        @if (IsAccessable("/api/Bas/CodingRule/Edit"))
        {
            <TableCellButton Size="Size.ExtraSmall" Icon="fa fa-pencil-square" Color="Color.Warning" Text="@WtmBlazor.Localizer["Sys.Edit"]" OnClick="()=>OnEditClick(context)" />
        }
        @if (IsAccessable("/api/Bas/CodingRule/{id}"))
        {
            <TableCellButton Size="Size.ExtraSmall" Icon="fa fa-info-circle" Color="Color.Info" Text="@WtmBlazor.Localizer["Page.详情"]" OnClick="()=>OnDetailsClick(context)" />
        }
        @if (IsAccessable("/api/Bas/CodingRule/BatchDelete"))
        {
            <PopConfirmButton OnConfirm="() => OnDeleteClick(context)" Size="Size.ExtraSmall" Icon="fa fa-trash" Color="Color.Danger" Text="@WtmBlazor.Localizer["Sys.Delete"]" Content="@WtmBlazor.Localizer["Sys.DeleteConfirm"]" CloseButtonText="@WtmBlazor.Localizer["Sys.Close"]" ConfirmButtonText="@WtmBlazor.Localizer["Sys.Delete"]"
                              ConfirmButtonColor="Color.Danger" />
        }
    </RowButtonTemplate>
    <TableToolbarTemplate>
        @if (IsAccessable("/api/Bas/CodingRule/Create"))
        {
            <TableToolbarButton TItem="nERP.ViewModel.Bas.CodingRuleVMs.CodingRule_View" Icon="fa fa-plus" Color="Color.Primary" Text="@WtmBlazor.Localizer["Sys.Create"]" OnClickCallback="(x)=>OnCreateClick()" />
        }
        @if (IsAccessable("/api/Bas/CodingRule/BatchDelete"))
        {
            <TableToolbarPopConfirmButton TItem="nERP.ViewModel.Bas.CodingRuleVMs.CodingRule_View"
                                          Icon="fa fa-trash" Color="Color.Danger" Text="@WtmBlazor.Localizer["Sys.Delete"]" Content="@WtmBlazor.Localizer["Sys.BatchDeleteConfirm"]" CloseButtonText="@WtmBlazor.Localizer["Sys.Close"]" ConfirmButtonText="@WtmBlazor.Localizer["Sys.BatchDelete"]"
                                          OnConfirm="@OnBatchDeleteClick" ConfirmButtonColor="Color.Danger" />
        }
        @if (IsAccessable("/api/Bas/CodingRule/BatchEdit"))
        {
            <TableToolbarButton TItem="nERP.ViewModel.Bas.CodingRuleVMs.CodingRule_View" Icon="fa fa-pencil-square" Color="Color.Primary" Text="@WtmBlazor.Localizer["Sys.BatchEdit"]" OnClickCallback="(x)=>OnBatchEditClick()" />
        }
        @if (IsAccessable("/api/Bas/CodingRule/Import"))
        {
            <TableToolbarButton TItem="nERP.ViewModel.Bas.CodingRuleVMs.CodingRule_View" Icon="fa fa-tasks" Color="Color.Primary" Text="@WtmBlazor.Localizer["Sys.Import"]" OnClickCallback="(x)=>OnImportClick()" />
        }
        @if (IsAccessable("/api/Bas/CodingRule/CodingRuleExportExcel"))
        {
            <TableToolbarButton TItem="nERP.ViewModel.Bas.CodingRuleVMs.CodingRule_View" Icon="fa fa-arrow-circle-down" Color="Color.Primary" Text="@WtmBlazor.Localizer["Sys.Export"]" OnClickCallback="(x)=>OnExportClick()" IsAsync="true" />
        }
    </TableToolbarTemplate>

</Table>



@code {
    [Parameter]
    public string id { get; set; }
    [Parameter]
    public string[] ids { get; set; }
    private CodingRuleListVM Model = new CodingRuleListVM();
    private Table<nERP.ViewModel.Bas.CodingRuleVMs.CodingRule_View> CodingRuleListVMdataTable;
    private Task OnResetSearchAsync(CodingRule_View item)
    {
        Model.Searcher = new CodingRuleSearcher();
        item = new CodingRule_View();
        return Task.CompletedTask;
    }
    private async Task CodingRuleListVMDoSearch()
    {
        await CodingRuleListVMdataTable.QueryAsync();
    }
    private async Task<QueryData<nERP.ViewModel.Bas.CodingRuleVMs.CodingRule_View>> CodingRuleListVMOnSearchCodingRule(QueryPageOptions opts)
    {

        return await StartSearch<nERP.ViewModel.Bas.CodingRuleVMs.CodingRule_View>("/api/Bas/CodingRule/SearchCodingRule", Model.Searcher, opts);
    }
    private async Task OnCreateClick()
    {
        var id = CodingRuleListVMdataTable.SelectedRows?.Select(x => x.ID.ToString())?.FirstOrDefault();
        if (await OpenDialog<Create>(@WtmBlazor.Localizer["Sys.Create"], x => x.id == (id ?? ""), isMax: false) == DialogResult.Yes)
        {
            await CodingRuleListVMdataTable.QueryAsync();
        }
    }

    private async Task OnEditClick(nERP.ViewModel.Bas.CodingRuleVMs.CodingRule_View item)
    {
        if (await OpenDialog<Edit>(@WtmBlazor.Localizer["Sys.Edit"], x => x.id == item.ID.ToString(), isMax: false) == DialogResult.Yes)
        {
            await CodingRuleListVMdataTable.QueryAsync();
        }
    }

    private async Task OnDetailsClick(nERP.ViewModel.Bas.CodingRuleVMs.CodingRule_View item)
    {
        if (await OpenDialog<Details>(@WtmBlazor.Localizer["Page.详情"], x => x.id == item.ID.ToString(), isMax: false) == DialogResult.Yes)
        {
            await CodingRuleListVMdataTable.QueryAsync();
        }
    }

    private async Task OnDeleteClick(nERP.ViewModel.Bas.CodingRuleVMs.CodingRule_View item)
    {
        await PostsData(new List<string> { item.ID.ToString() }, $"/api/Bas/CodingRule/batchdelete", (s) => "Sys.OprationSuccess");
        await CodingRuleListVMdataTable.QueryAsync();
    }

    private async Task OnBatchDeleteClick()
    {
        if (CodingRuleListVMdataTable.SelectedRows?.Any() == true)
        {
            await PostsData(CodingRuleListVMdataTable.SelectedRows.Select(x => x.ID.ToString()).ToList(), $"/api/Bas/CodingRule/batchdelete", (s) => WtmBlazor.Localizer["Sys.BatchDeleteSuccess", s]);
            await CodingRuleListVMdataTable.QueryAsync();
        }
        else
        {
            await WtmBlazor.Toast.Information(WtmBlazor.Localizer["Sys.Info"], WtmBlazor.Localizer["Sys.SelectOneRowMin"]);
        }
    }

    private async Task OnBatchEditClick()
    {
        if (CodingRuleListVMdataTable.SelectedRows?.Any() == true)
        {
            if (await OpenDialog<BatchEdit>(WtmBlazor.Localizer["Sys.BatchEdit"], x => x.ids == CodingRuleListVMdataTable.SelectedRows.Select(x => x.ID.ToString()).ToArray(), isMax: false) == DialogResult.Yes)
            {
                await CodingRuleListVMdataTable.QueryAsync();
            }
        }
        else
        {
            await WtmBlazor.Toast.Information(WtmBlazor.Localizer["Sys.Info"], WtmBlazor.Localizer["Sys.SelectOneRowMin"]);
        }
    }

    private async Task OnImportClick()
    {
        var id = CodingRuleListVMdataTable.SelectedRows?.Select(x => x.ID.ToString())?.FirstOrDefault();
        if (await OpenDialog<Import>(@WtmBlazor.Localizer["Sys.Import"], x => x.id == (id ?? ""), isMax: false) == DialogResult.Yes)
        {
            await CodingRuleListVMdataTable.QueryAsync();
        }
    }

    private async Task OnExportClick()
    {
        if (CodingRuleListVMdataTable.SelectedRows?.Any() == true)
        {
            await Download("/api/Bas/CodingRule/CodingRuleExportExcelByIds", CodingRuleListVMdataTable.SelectedRows.Select(x => x.ID.ToString()).ToList());
        }
        else
        {
            await Download("/api/Bas/CodingRule/CodingRuleExportExcel", Model.Searcher);
        }
    }


    private List<SelectedItem> AllCodingLists = new List<SelectedItem>();
    protected override async Task OnInitializedAsync()
    {


        AllCodingLists = await WtmBlazor.Api.CallItemsApi("/api/Bas/CodingRule/GetCodingLists");
        AllCodingLists.Insert(0, new SelectedItem { Text = WtmBlazor.Localizer["Sys.PleaseSelect"], Value = "" });

        await base.OnInitializedAsync();
    }
}
